import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:lib_uikit/lib_uikit.dart';

/// @Title   自定义RichText（1：使用自定义字体，2：去除上下边距，3：新增支持行间距）
/// @Author: bryce
/// @CreateDate: 2023年10月16日16:07:12
class GTRichText extends StatelessWidget {
  final InlineSpan text;
  final TextAlign textAlign;
  final TextDirection? textDirection;
  final StrutStyle? strutStyle;
  final double? fontSize;
  final FontWeight? fontWeight;
  final TextOverflow overflow;
  final int? maxLines;
  final Color? selectionColor;
  final FontStyle? fontStyle;
  final bool softWrap;
  final double textScaleFactor;
  final Locale? locale;
  final TextWidthBasis textWidthBasis;
  final SelectionRegistrar? selectionRegistrar;

  /// 行间距数值 默认4
  final double lineSpace;

  /// 是否移除默认的auto上下边距 默认false不去除
  final bool removeTopBottomPadding;
  final EdgeInsetsGeometry padding;

  const GTRichText(
      {super.key,
      required this.text,
      this.textAlign = TextAlign.start,
      this.textDirection,
      this.strutStyle,
      this.fontSize,
      this.fontWeight,
      this.overflow = TextOverflow.clip,
      this.maxLines,
      this.fontStyle,
      this.lineSpace = 4,
      this.selectionColor,
      this.softWrap = true,
      this.textScaleFactor = 1.0,
      this.locale,
      this.textWidthBasis = TextWidthBasis.parent,
      this.selectionRegistrar,
      this.removeTopBottomPadding = false,
      this.padding = EdgeInsets.zero});

  @override
  Widget build(BuildContext context) {
    double mFontSize = strutStyle?.fontSize ?? fontSize ?? 16;
    StrutStyle mStyle = gtStrutStyle(
      fontSize: strutStyle?.fontSize ?? fontSize,
      fontWeight: strutStyle?.fontWeight ?? fontWeight ?? FontWeight.w400,
      fontStyle: strutStyle?.fontStyle ?? fontStyle,
      height: strutStyle?.height ?? (lineSpace / mFontSize) + 1,
      leadingDistribution: strutStyle?.leadingDistribution,
      leading: strutStyle?.leading,
      forceStrutHeight: strutStyle?.forceStrutHeight,
      debugLabel: strutStyle?.debugLabel,
    );

    return Padding(
      padding: padding,
      child: RichText(
        text: text,
        textAlign: textAlign,
        textDirection: textDirection,
        overflow: overflow,
        maxLines: maxLines,
        selectionColor: selectionColor,
        strutStyle: mStyle,
        softWrap: softWrap,
        textScaleFactor: textScaleFactor,
        locale: locale,
        textWidthBasis: textWidthBasis,
        selectionRegistrar: selectionRegistrar,
        textHeightBehavior: TextHeightBehavior(
            applyHeightToLastDescent: !removeTopBottomPadding, applyHeightToFirstAscent: !removeTopBottomPadding),
        // strutStyle: StrutStyle(fontSize: mFontSize, leading: (lineSpace / mFontSize)),
      ),
    );
  }
}
